package com.dlc.demo.daiqianma.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密字符串（不带秘钥）
 */
public class MD5Util {
    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");

            messageDigest.reset();

            byte[] a = str.getBytes();
            byte[] b = new byte[a.length * 2];
            for(int i=0;i < a.length; i ++)
            {
                b[i * 2] = a[i];
                b[i * 2 + 1] = 0;
            }
            messageDigest.update(b);
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException caught!");
            System.exit(-1);
        }
        byte[] m = messageDigest.digest();//
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < m.length; i ++){
            sb.append(Integer.toHexString(0xFF & m[i]));
        }
        return sb.toString();
    }

}
